﻿using DatabaseCore.Sqlite;
using Service.src.com.utils.modal;
using Service.src.Flow.Server;
using Service.src.NW.Model;
using System.Collections.Generic;
using System.Web.Http;

namespace NW_WEB_Service.com.app.controllers
{

    [RoutePrefix("api/event")]
    public class EventController: ApiController
    {

        /// <summary>
        /// 查找事件
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("list")]
        public HttpResult Gets()
        {
            EventServer ES = new EventServer();
            List<SystemEvent> Events = ES.SearchEvent(SqliteHelper.SystemDefaultDBID);

            return new HttpResult().Success(Events);
        }

        /// <summary>
        /// 新增事件
        /// </summary>
        /// <param name="EventJson"></param>
        /// <returns></returns>
        [HttpPost]
        [Route]
        public HttpResult Save([FromBody] SystemEvent _Event)
        {
            HttpResult result = new HttpResult();
            EventServer ES = new EventServer();
            try
            {
                SystemEvent Event = ES.AddEvent(SqliteHelper.SystemDefaultDBID, _Event);
                result.Success(Event);
            }
            catch (System.Exception ex)
            {
                result.Error(ex.Message);
            }
            return result;
        }

        /// <summary>
        /// 更新事件
        /// </summary>
        /// <param name="EventJson"></param>
        /// <returns></returns>
        [HttpPut]
        [Route]
        public HttpResult UpdateEvent([FromBody] SystemEvent _Event)
        {
            EventServer Event = new EventServer();
            SystemEvent Res = Event.UpdateEvent(SqliteHelper.SystemDefaultDBID, _Event);
            return new HttpResult().Success(Res);
        }

        /// <summary>
        /// 根据ID删除事件
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete]
        [Route("{Id:int}")]
        public HttpResult DeleteEvent(int Id)
        {
            EventServer Event = new EventServer();
            bool Res = Event.DeleteEvent(SqliteHelper.SystemDefaultDBID, Id);
            return new HttpResult().Success(Res);
        }
    }
}